home *** CD-ROM | disk | FTP | other *** search
/ Micromanía 92 / CDMM92_1.ISO / SOF 2 SDK / sof2sdk-101.msi / _92D6AC311BB48EBA344BBABC89DA6AB0 / _E209A87AF9D54EA59FE6A5D0E454C09F < prev    next >
Encoding:
Text File  |  2002-06-19  |  38.5 KB  |  1,456 lines

  1. // Copyright (C) 2001-2002 Raven Software.
  2. //
  3. // cg_view.c -- setup all the parameters (position, angle, etc)
  4. // for a 3D rendering
  5. #include "cg_local.h"
  6.  
  7. #if !defined(CL_LIGHT_H_INC)
  8.     #include "cg_lights.h"
  9. #endif
  10.  
  11. /*
  12. =============================================================================
  13.  
  14.   MODEL TESTING
  15.  
  16. The viewthing and gun positioning tools from Q2 have been integrated and
  17. enhanced into a single model testing facility.
  18.  
  19. Model viewing can begin with either "testmodel <modelname>" or "testgun <modelname>".
  20.  
  21. The names must be the full pathname after the basedir, like 
  22. "models/weapons/v_launch/tris.md3" or "players/male/tris.md3"
  23.  
  24. Testmodel will create a fake entity 100 units in front of the current view
  25. position, directly facing the viewer.  It will remain immobile, so you can
  26. move around it to view it from different angles.
  27.  
  28. Testgun will cause the model to follow the player around and supress the real
  29. view weapon model.  The default frame 0 of most guns is completely off screen,
  30. so you will probably have to cycle a couple frames to see it.
  31.  
  32. "nextframe", "prevframe", "nextskin", and "prevskin" commands will change the
  33. frame or skin of the testmodel.  These are bound to F5, F6, F7, and F8 in
  34. q3default.cfg.
  35.  
  36. If a gun is being tested, the "gun_x", "gun_y", and "gun_z" variables will let
  37. you adjust the positioning.
  38.  
  39. Note that none of the model testing features update while the game is paused, so
  40. it may be convenient to test with deathmatch set to 1 so that bringing down the
  41. console doesn't pause the game.
  42.  
  43. =============================================================================
  44. */
  45.  
  46. /*
  47. =================
  48. CG_TestModel_f
  49.  
  50. Creates an entity in front of the current position, which
  51. can then be moved around
  52. =================
  53. */
  54. void CG_TestModel_f (void) 
  55. {
  56.     vec3_t        angles;
  57.  
  58.     memset( &cg.testModelEntity, 0, sizeof(cg.testModelEntity) );
  59.     if ( trap_Argc() < 2 ) 
  60.     {
  61.         return;
  62.     }
  63.  
  64.     Q_strncpyz (cg.testModelName, CG_Argv( 1 ), MAX_QPATH );
  65.     cg.testModelEntity.hModel = trap_R_RegisterModel( cg.testModelName );
  66.  
  67.     if ( trap_Argc() == 3 ) 
  68.     {
  69.         cg.testModelEntity.backlerp = atof( CG_Argv( 2 ) );
  70.         cg.testModelEntity.frame = 1;
  71.         cg.testModelEntity.oldframe = 0;
  72.     }
  73.     
  74.     if (! cg.testModelEntity.hModel ) 
  75.     {
  76.         Com_Printf( "Can't register model\n" );
  77.         return;
  78.     }
  79.  
  80.     VectorMA( cg.refdef.vieworg, 100, cg.refdef.viewaxis[0], cg.testModelEntity.origin );
  81.  
  82.     angles[PITCH] = 0;
  83.     angles[YAW] = 180 + cg.refdef.viewangles[1];
  84.     angles[ROLL] = 0;
  85.  
  86.     AnglesToAxis( angles, cg.testModelEntity.axis );
  87. }
  88.  
  89. void CG_TestModelNextFrame_f (void) 
  90. {
  91.     cg.testModelEntity.frame++;
  92.     Com_Printf( "frame %i\n", cg.testModelEntity.frame );
  93. }
  94.  
  95. void CG_TestModelPrevFrame_f (void) 
  96. {
  97.     cg.testModelEntity.frame--;
  98.     if ( cg.testModelEntity.frame < 0 ) 
  99.     {
  100.         cg.testModelEntity.frame = 0;
  101.     }
  102.  
  103.     Com_Printf( "frame %i\n", cg.testModelEntity.frame );
  104. }
  105.  
  106. void CG_TestModelNextSkin_f (void) 
  107. {
  108.     cg.testModelEntity.skinNum++;
  109.     Com_Printf( "skin %i\n", cg.testModelEntity.skinNum );
  110. }
  111.  
  112. void CG_TestModelPrevSkin_f (void) 
  113. {
  114.     cg.testModelEntity.skinNum--;
  115.     if ( cg.testModelEntity.skinNum < 0 ) 
  116.     {
  117.         cg.testModelEntity.skinNum = 0;
  118.     }
  119.     Com_Printf( "skin %i\n", cg.testModelEntity.skinNum );
  120. }
  121.  
  122. static void CG_AddTestModel (void) 
  123. {
  124.     // re-register the model, because the level may have changed
  125.     cg.testModelEntity.hModel = trap_R_RegisterModel( cg.testModelName );
  126.     if (! cg.testModelEntity.hModel ) 
  127.     {
  128.         Com_Printf ("Can't register model\n");
  129.         return;
  130.     }
  131.  
  132.     trap_R_AddRefEntityToScene( &cg.testModelEntity );
  133. }
  134.  
  135. /*
  136. =================
  137. CG_CalcVrect
  138.  
  139. Sets the coordinates of the rendered window
  140. =================
  141. */
  142. static void CG_CalcVrect (void) 
  143. {
  144.     int        size;
  145.  
  146.     size = 100;
  147.  
  148.     cg.refdef.width = cgs.glconfig.vidWidth*size/100;
  149.     cg.refdef.width &= ~1;
  150.  
  151.     cg.refdef.height = cgs.glconfig.vidHeight*size/100;
  152.     cg.refdef.height &= ~1;
  153.  
  154.     cg.refdef.x = (cgs.glconfig.vidWidth - cg.refdef.width)/2;
  155.     cg.refdef.y = (cgs.glconfig.vidHeight - cg.refdef.height)/2;
  156. }
  157.  
  158. //==============================================================================
  159.  
  160. //==============================================================================
  161. //==============================================================================
  162. // this causes a compiler bug on mac MrC compiler
  163. static void CG_StepOffset( void ) 
  164. {
  165.     int        timeDelta;
  166.     
  167.     // smooth out stair climbing
  168.     timeDelta = cg.time - cg.stepTime;
  169.     if ( timeDelta < STEP_TIME ) 
  170.     {
  171.         cg.refdef.vieworg[2] -= cg.stepChange * (STEP_TIME - timeDelta) / STEP_TIME;
  172.     }
  173. }
  174.  
  175. #define CAMERA_DAMP_INTERVAL    50
  176.  
  177. static vec3_t    cameramins = { -4, -4, -4 };
  178. static vec3_t    cameramaxs = { 4, 4, 4 };
  179. vec3_t    camerafwd, cameraup;
  180.  
  181. vec3_t    cameraFocusAngles,            cameraFocusLoc;
  182. vec3_t    cameraIdealTarget,            cameraIdealLoc;
  183. vec3_t    cameraCurTarget={0,0,0},    cameraCurLoc={0,0,0};
  184. vec3_t    cameraOldLoc={0,0,0},        cameraNewLoc={0,0,0};
  185. int        cameraLastFrame=0;
  186.  
  187. /*
  188. ===============
  189. Notes on the camera viewpoint in and out...
  190.  
  191. cg.refdef.vieworg
  192. --at the start of the function holds the player actor's origin (center of player model).
  193. --it is set to the final view location of the camera at the end of the camera code.
  194. cg.refdef.viewangles
  195. --at the start holds the client's view angles
  196. --it is set to the final view angle of the camera at the end of the camera code.
  197.  
  198. ===============
  199. */
  200.   
  201. /*
  202. ===============
  203. CG_CalcTargetThirdPersonViewLocation
  204.  
  205. ===============
  206. */
  207. static void CG_CalcIdealThirdPersonViewTarget(void)
  208. {
  209.     // Initialize IdealTarget
  210.     VectorCopy(cg.refdef.vieworg, cameraFocusLoc);
  211.  
  212.     // Add in the new viewheight
  213.     cameraFocusLoc[2] += cg.snap->ps.viewheight;
  214.  
  215.     // Add in a vertical offset from the viewpoint, which puts the actual target above the head, regardless of angle.
  216.     VectorMA(cameraFocusLoc, 1, cameraup, cameraIdealTarget);
  217. }
  218.  
  219.     
  220.  
  221. /*
  222. ===============
  223. CG_CalcTargetThirdPersonViewLocation
  224.  
  225. ===============
  226. */
  227. static void CG_CalcIdealThirdPersonViewLocation(void)
  228. {
  229.     VectorMA(cameraIdealTarget, -(cg_thirdPersonRange.value), camerafwd, cameraIdealLoc);
  230. }
  231.  
  232.  
  233.  
  234. static void CG_ResetThirdPersonViewDamp(void)
  235. {
  236.     trace_t trace;
  237.  
  238.     // Cap the pitch within reasonable limits
  239.     if (cameraFocusAngles[PITCH] > 89.0)
  240.     {
  241.         cameraFocusAngles[PITCH] = 89.0;
  242.     }
  243.     else if (cameraFocusAngles[PITCH] < -89.0)
  244.     {
  245.         cameraFocusAngles[PITCH] = -89.0;
  246.     }
  247.  
  248.     AngleVectors(cameraFocusAngles, camerafwd, NULL, cameraup);
  249.  
  250.     // Set the cameraIdealTarget
  251.     CG_CalcIdealThirdPersonViewTarget();
  252.  
  253.     // Set the cameraIdealLoc
  254.     CG_CalcIdealThirdPersonViewLocation();
  255.  
  256.     // Now, we just set everything to the new positions.
  257.     VectorCopy(cameraIdealLoc, cameraCurLoc);
  258.     VectorCopy(cameraIdealTarget, cameraCurTarget);
  259.  
  260.     // First thing we do is trace from the first person viewpoint out to the new target location.
  261.     CG_Trace(&trace, cameraFocusLoc, cameramins, cameramaxs, cameraCurTarget, cg.snap->ps.clientNum, MASK_SOLID|CONTENTS_PLAYERCLIP);
  262.     if (trace.fraction <= 1.0)
  263.     {
  264.         VectorCopy(trace.endpos, cameraCurTarget);
  265.     }
  266.  
  267.     // Now we trace from the new target location to the new view location, to make sure there is nothing in the way.
  268.     CG_Trace(&trace, cameraCurTarget, cameramins, cameramaxs, cameraCurLoc, cg.snap->ps.clientNum, MASK_SOLID|CONTENTS_PLAYERCLIP);
  269.     if (trace.fraction <= 1.0)
  270.     {
  271.         VectorCopy(trace.endpos, cameraCurLoc);
  272.     }
  273.  
  274.     cameraLastFrame = cg.time;
  275. }
  276.  
  277. // This is called every frame.
  278. static void CG_UpdateThirdPersonTargetDamp(void)
  279. {
  280.     trace_t trace;
  281.     vec3_t    targetdiff;
  282.     float    dampfactor, dtime, ratio;
  283.     float    damp = 1.0f;
  284.  
  285.     // Set the cameraIdealTarget
  286.     // Automatically get the ideal target, to avoid jittering.
  287.     CG_CalcIdealThirdPersonViewTarget();
  288.  
  289.     if ( damp >=1.0)
  290.     {    // No damping.
  291.         VectorCopy(cameraIdealTarget, cameraCurTarget);
  292.     }
  293.     else if ( damp >=0.0)
  294.     {    
  295.         // Calculate the difference from the current position to the new one.
  296.         VectorSubtract(cameraIdealTarget, cameraCurTarget, targetdiff);
  297.  
  298.         // Now we calculate how much of the difference we cover in the time allotted.
  299.         // The equation is (Damp)^(time)
  300.         dampfactor = 1.0-damp;    // We must exponent the amount LEFT rather than the amount bled off
  301.         dtime = (float)(cg.time-cameraLastFrame) * (1.0/(float)CAMERA_DAMP_INTERVAL);    // Our dampfactor is geared towards a time interval equal to "1".
  302.  
  303.         // Note that since there are a finite number of "practical" delta millisecond values possible, 
  304.         // the ratio should be initialized into a chart ultimately.
  305.         ratio = powf(dampfactor, (int)dtime);
  306.         
  307.         // This value is how much distance is "left" from the ideal.
  308.         VectorMA(cameraIdealTarget, -ratio, targetdiff, cameraCurTarget);
  309.         /////////////////////////////////////////////////////////////////////////////////////////////////////////
  310.     }
  311.  
  312.     // Now we trace to see if the new location is cool or not.
  313.  
  314.     // First thing we do is trace from the first person viewpoint out to the new target location.
  315.     CG_Trace(&trace, cameraFocusLoc, cameramins, cameramaxs, cameraCurTarget, cg.snap->ps.clientNum, MASK_SOLID|CONTENTS_PLAYERCLIP);
  316.     if (trace.fraction < 1.0)
  317.     {
  318.         VectorCopy(trace.endpos, cameraCurTarget);
  319.     }
  320.  
  321.     // Note that previously there was an upper limit to the number of physics traces that are done through the world
  322.     // for the sake of camera collision, since it wasn't calced per frame.  Now it is calculated every frame.
  323.     // This has the benefit that the camera is a lot smoother now (before it lerped between tested points),
  324.     // however two full volume traces each frame is a bit scary to think about.
  325. }
  326.  
  327. // This can be called every interval, at the user's discretion.
  328. static void CG_UpdateThirdPersonCameraDamp(void)
  329. {
  330.     trace_t trace;
  331.     vec3_t    locdiff;
  332.     float dampfactor, dtime, ratio;
  333.     float damp = 1.0f;
  334.  
  335.     // Set the cameraIdealLoc
  336.     CG_CalcIdealThirdPersonViewLocation();
  337.     
  338.     
  339.     // First thing we do is calculate the appropriate damping factor for the camera.
  340.     dampfactor=0.0;
  341.     if (damp != 0.0)
  342.     {
  343.         double pitch;
  344.  
  345.         // Note that the camera pitch has already been capped off to 89.
  346.         pitch = Q_fabs(cameraFocusAngles[PITCH]);
  347.  
  348.         // The higher the pitch, the larger the factor, so as you look up, it damps a lot less.
  349.         pitch /= 89.0;    
  350.         dampfactor = (1.0-damp)*(pitch*pitch);
  351.  
  352.         dampfactor += damp;
  353.     }
  354.  
  355.     if (dampfactor>=1.0)
  356.     {    // No damping.
  357.         VectorCopy(cameraIdealLoc, cameraCurLoc);
  358.     }
  359.     else if (dampfactor>=0.0)
  360.     {    
  361.         // Calculate the difference from the current position to the new one.
  362.         VectorSubtract(cameraIdealLoc, cameraCurLoc, locdiff);
  363.  
  364.         // Now we calculate how much of the difference we cover in the time allotted.
  365.         // The equation is (Damp)^(time)
  366.         dampfactor = 1.0-dampfactor;    // We must exponent the amount LEFT rather than the amount bled off
  367.         dtime = (float)(cg.time-cameraLastFrame) * (1.0/(float)CAMERA_DAMP_INTERVAL);    // Our dampfactor is geared towards a time interval equal to "1".
  368.  
  369.         // Note that since there are a finite number of "practical" delta millisecond values possible, 
  370.         // the ratio should be initialized into a chart ultimately.
  371.         ratio = powf(dampfactor, (int)dtime);
  372.         
  373.         // This value is how much distance is "left" from the ideal.
  374.         VectorMA(cameraIdealLoc, -ratio, locdiff, cameraCurLoc);
  375.         /////////////////////////////////////////////////////////////////////////////////////////////////////////
  376.     }
  377.  
  378.     // Now we trace from the new target location to the new view location, to make sure there is nothing in the way.
  379.     CG_Trace(&trace, cameraCurTarget, cameramins, cameramaxs, cameraCurLoc, cg.snap->ps.clientNum, MASK_SOLID|CONTENTS_PLAYERCLIP);
  380.     if (trace.fraction < 1.0)
  381.     {
  382.         VectorCopy( trace.endpos, cameraCurLoc );
  383.         //FIXME: when the trace hits movers, it gets very very jaggy... ?
  384.         /*
  385.         //this doesn't actually help any
  386.         if ( trace.entityNum != ENTITYNUM_WORLD )
  387.         {
  388.             centity_t *cent = &cg_entities[trace.entityNum];
  389.             gentity_t *gent = &g_entities[trace.entityNum];
  390.             if ( cent != NULL && gent != NULL )
  391.             {
  392.                 if ( cent->currentState.pos.trType == TR_LINEAR || cent->currentState.pos.trType == TR_LINEAR_STOP )
  393.                 {
  394.                     vec3_t    diff;
  395.                     VectorSubtract( cent->lerpOrigin, gent->currentOrigin, diff );
  396.                     VectorAdd( cameraCurLoc, diff, cameraCurLoc );
  397.                 }
  398.             }
  399.         }
  400.         */
  401.     }
  402.  
  403.     // Note that previously there was an upper limit to the number of physics traces that are done through the world
  404.     // for the sake of camera collision, since it wasn't calced per frame.  Now it is calculated every frame.
  405.     // This has the benefit that the camera is a lot smoother now (before it lerped between tested points),
  406.     // however two full volume traces each frame is a bit scary to think about.
  407. }
  408.  
  409. /*
  410. ===============`
  411. CG_OffsetThirdPersonView
  412.  
  413. ===============
  414. */
  415. extern vmCvar_t cg_thirdPersonHorzOffset;
  416. static void CG_OffsetThirdPersonView( void ) 
  417. {
  418.     vec3_t diff;
  419.  
  420.     // Set camera viewing direction.
  421.     VectorCopy( cg.refdef.viewangles, cameraFocusAngles );
  422.  
  423.     // if dead, look at killer
  424.     if ( 0 && cg.snap->ps.stats[STAT_HEALTH] <= 0 ) 
  425.     {
  426.         cameraFocusAngles[YAW] = cg.snap->ps.stats[STAT_DEAD_YAW];
  427.         cameraFocusAngles[PITCH] = 90;
  428.     }
  429.     else
  430.     {    // Add in the third Person Angle.
  431.         cameraFocusAngles[YAW] += cg_thirdPersonYaw.value;
  432.         cameraFocusAngles[PITCH] += cg_thirdPersonPitch.value;
  433.     }
  434.  
  435.     // The next thing to do is to see if we need to calculate a new camera target location.
  436.  
  437.     // If we went back in time for some reason, or if we just started, reset the sample.
  438.     if (cameraLastFrame == 0 || cameraLastFrame > cg.time)
  439.     {
  440.         CG_ResetThirdPersonViewDamp();
  441.     }
  442.     else
  443.     {
  444.         // Cap the pitch within reasonable limits
  445.         if (cameraFocusAngles[PITCH] > 89.0)
  446.         {
  447.             cameraFocusAngles[PITCH] = 89.0;
  448.         }
  449.         else if (cameraFocusAngles[PITCH] < -89.0)
  450.         {
  451.             cameraFocusAngles[PITCH] = -89.0;
  452.         }
  453.  
  454.         AngleVectors(cameraFocusAngles, camerafwd, NULL, cameraup);
  455.  
  456.         // Move the target to the new location.
  457.         CG_UpdateThirdPersonTargetDamp();
  458.         CG_UpdateThirdPersonCameraDamp();
  459.     }
  460.  
  461.     // Now interestingly, the Quake method is to calculate a target focus point above the player, and point the camera at it.
  462.     // We won't do that for now.
  463.  
  464.     // We must now take the angle taken from the camera target and location.
  465.     VectorSubtract(cameraCurTarget, cameraCurLoc, diff);
  466.     VectorNormalize(diff);
  467.     vectoangles(diff, cg.refdef.viewangles);
  468.  
  469.     // Temp: just move the camera to the side a bit
  470.     if ( cg_thirdPersonHorzOffset.value != 0.0f )
  471.     {
  472.         AnglesToAxis( cg.refdef.viewangles, cg.refdef.viewaxis );
  473.         VectorMA( cameraCurLoc, cg_thirdPersonHorzOffset.value, cg.refdef.viewaxis[1], cameraCurLoc );
  474.     }
  475.  
  476.     // ...and of course we should copy the new view location to the proper spot too.
  477.     VectorCopy(cameraCurLoc, cg.refdef.vieworg);
  478.  
  479.     cameraLastFrame=cg.time;
  480. }
  481.  
  482.  
  483.  
  484. /*
  485. ===============
  486. CG_OffsetThirdPersonView
  487.  
  488. ===============
  489. *//*
  490. #define    FOCUS_DISTANCE    512
  491. static void CG_OffsetThirdPersonView( void ) {
  492.     vec3_t        forward, right, up;
  493.     vec3_t        view;
  494.     vec3_t        focusAngles;
  495.     trace_t        trace;
  496.     static vec3_t    mins = { -4, -4, -4 };
  497.     static vec3_t    maxs = { 4, 4, 4 };
  498.     vec3_t        focusPoint;
  499.     float        focusDist;
  500.     float        forwardScale, sideScale;
  501.  
  502.     cg.refdef.vieworg[2] += cg.predictedPlayerState.viewheight;
  503.  
  504.     VectorCopy( cg.refdef.viewangles, focusAngles );
  505.  
  506.     // if dead, look at killer
  507.     if ( cg.predictedPlayerState.stats[STAT_HEALTH] <= 0 ) {
  508.         focusAngles[YAW] = cg.predictedPlayerState.stats[STAT_DEAD_YAW];
  509.         cg.refdef.viewangles[YAW] = cg.predictedPlayerState.stats[STAT_DEAD_YAW];
  510.     }
  511.  
  512.     if ( focusAngles[PITCH] > 45 ) {
  513.         focusAngles[PITCH] = 45;        // don't go too far overhead
  514.     }
  515.     AngleVectors( focusAngles, forward, NULL, NULL );
  516.  
  517.     VectorMA( cg.refdef.vieworg, FOCUS_DISTANCE, forward, focusPoint );
  518.  
  519.     VectorCopy( cg.refdef.vieworg, view );
  520.  
  521.     view[2] += 8;
  522.  
  523.     cg.refdef.viewangles[PITCH] *= 0.5;
  524.  
  525.     AngleVectors( cg.refdef.viewangles, forward, right, up );
  526.  
  527.     forwardScale = cos( cg_thirdPersonAngle.value / 180 * M_PI );
  528.     sideScale = sin( cg_thirdPersonAngle.value / 180 * M_PI );
  529.     VectorMA( view, -cg_thirdPersonRange.value * forwardScale, forward, view );
  530.     VectorMA( view, -cg_thirdPersonRange.value * sideScale, right, view );
  531.  
  532.     // trace a ray from the origin to the viewpoint to make sure the view isn't
  533.     // in a solid block.  Use an 8 by 8 block to prevent the view from near clipping anything
  534.  
  535.     if (!cg_cameraMode.integer) {
  536.         CG_Trace( &trace, cg.refdef.vieworg, mins, maxs, view, cg.predictedPlayerState.clientNum, MASK_SOLID );
  537.  
  538.         if ( trace.fraction != 1.0 ) {
  539.             VectorCopy( trace.endpos, view );
  540.             view[2] += (1.0 - trace.fraction) * 32;
  541.             // try another trace to this position, because a tunnel may have the ceiling
  542.             // close enogh that this is poking out
  543.  
  544.             CG_Trace( &trace, cg.refdef.vieworg, mins, maxs, view, cg.predictedPlayerState.clientNum, MASK_SOLID );
  545.             VectorCopy( trace.endpos, view );
  546.         }
  547.     }
  548.  
  549.  
  550.     VectorCopy( view, cg.refdef.vieworg );
  551.  
  552.     // select pitch to look at focus point from vieword
  553.     VectorSubtract( focusPoint, cg.refdef.vieworg, focusPoint );
  554.     focusDist = sqrt( focusPoint[0] * focusPoint[0] + focusPoint[1] * focusPoint[1] );
  555.     if ( focusDist < 1 ) {
  556.         focusDist = 1;    // should never happen
  557.     }
  558.     cg.refdef.viewangles[PITCH] = -180 / M_PI * atan2( focusPoint[2], focusDist );
  559.     cg.refdef.viewangles[YAW] -= cg_thirdPersonAngle.value;
  560. }
  561.  
  562.  
  563. // this causes a compiler bug on mac MrC compiler
  564. static void CG_StepOffset( void ) {
  565.     int        timeDelta;
  566.     
  567.     // smooth out stair climbing
  568.     timeDelta = cg.time - cg.stepTime;
  569.     if ( timeDelta < STEP_TIME ) {
  570.         cg.refdef.vieworg[2] -= cg.stepChange 
  571.             * (STEP_TIME - timeDelta) / STEP_TIME;
  572.     }
  573. }*/
  574.  
  575. /*
  576. ===============
  577. CG_OffsetFirstPersonView
  578. ===============
  579. */
  580. static void CG_OffsetFirstPersonView( void ) 
  581. {
  582.     float    *origin;
  583.     float    *angles;
  584.     float    bob;
  585.     float    ratio;
  586.     float    delta;
  587.     float    speed;
  588.     float    f;
  589.     vec3_t    predictedVelocity;
  590.     int        timeDelta;
  591.     vec3_t    right;
  592.  
  593.     if ( cg.snap->ps.pm_type == PM_INTERMISSION ) {
  594.         return;
  595.     }
  596.  
  597.     origin = cg.refdef.vieworg;
  598.     angles = cg.refdef.viewangles;
  599.  
  600.     // If dead, fall to the ground
  601.     if ( cg.snap->ps.pm_type == PM_DEAD )
  602.     {
  603.         int        atime;
  604.         int        htime;
  605.         float    f;
  606.         
  607.         atime = (cg.time - cg.deathTime);
  608.         if ( atime > 450)
  609.         {
  610.             atime = 450;
  611.         }
  612.  
  613.         htime = (cg.time - cg.deathTime);
  614.         if ( htime > 350)
  615.         {
  616.             htime = 350;
  617.         }
  618.  
  619.         f = (float)atime / 450.0f;
  620.         cg.refdef.viewangles[ROLL] += ((cg.deathTime&0x100)?-1:1) * 40 * f;
  621.         cg.refdef.viewangles[PITCH] -= ((cg.deathTime&0x010)?-1:1) * 20 * f;
  622.         cg.refdef.viewangles[YAW] -= ((cg.deathTime&0x001)?-1:1) * 40 * f;
  623.  
  624.         f = (float)htime / 400.0f;
  625.         cg.refdef.vieworg[2] += (f * cg.predictedPlayerState.viewheight * 0.90f);
  626.  
  627.         return;
  628.     }
  629.  
  630.     // add angles based on damage kick
  631.     if ( cg.damageTime ) 
  632.     {
  633.         ratio = cg.time - cg.damageTime;
  634.         if ( ratio < DAMAGE_DEFLECT_TIME ) 
  635.         {
  636.             ratio /= DAMAGE_DEFLECT_TIME;
  637.             angles[PITCH] += ratio * cg.v_dmg_pitch;
  638.  
  639.             if ( cg.v_dmg_roll != 255 )
  640.             {
  641.                 angles[ROLL] += ratio * cg.v_dmg_roll;
  642.             }
  643.         } 
  644.         else 
  645.         {
  646.             ratio = 1.0 - ( ratio - DAMAGE_DEFLECT_TIME ) / DAMAGE_RETURN_TIME;
  647.             if ( ratio > 0 ) 
  648.             {
  649.                 angles[PITCH] += ratio * cg.v_dmg_pitch;
  650.  
  651.                 if ( cg.v_dmg_roll != 255 )
  652.                 {
  653.                     angles[ROLL] += ratio * cg.v_dmg_roll;
  654.                 }
  655.             }
  656.         }
  657.     }
  658.  
  659.     // add pitch based on fall kick
  660. #if 0
  661.     ratio = ( cg.time - cg.landTime) / FALL_TIME;
  662.     if (ratio < 0)
  663.         ratio = 0;
  664.     angles[PITCH] += ratio * cg.fall_value;
  665. #endif
  666.  
  667.     // add angles based on velocity
  668.     VectorCopy( cg.predictedPlayerState.velocity, predictedVelocity );
  669.  
  670.     delta = DotProduct ( predictedVelocity, cg.refdef.viewaxis[0]);
  671.     angles[PITCH] += delta * cg_runpitch.value;
  672.     
  673.     delta = DotProduct ( predictedVelocity, cg.refdef.viewaxis[1]);
  674.     angles[ROLL] -= delta * cg_runroll.value;
  675.  
  676.     // add angles based on bob
  677.  
  678.     // make sure the bob is visible even at low speeds
  679.     speed = cg.xyspeed > 200 ? cg.xyspeed : 200;
  680.  
  681.     delta = cg.bobfracsin * cg_bobpitch.value * speed;
  682.     if ((cg.predictedPlayerState.pm_flags & PMF_DUCKED) && (cg.predictedPlayerState.groundEntityNum!=ENTITYNUM_NONE))
  683.         delta *= 3;        // crouching
  684.     angles[PITCH] += delta;
  685.     delta = cg.bobfracsin * cg_bobroll.value * speed;
  686.     if ((cg.predictedPlayerState.pm_flags & PMF_DUCKED) && (cg.predictedPlayerState.groundEntityNum!=ENTITYNUM_NONE))
  687.         delta *= 3;        // crouching accentuates roll
  688.     if (cg.bobcycle & 1)
  689.         delta = -delta;
  690.     angles[ROLL] += delta;
  691.  
  692. //===================================
  693.  
  694.     // add view height
  695.     origin[2] += cg.predictedPlayerState.viewheight;
  696.  
  697.     // smooth out duck height changes
  698.     timeDelta = cg.time - cg.duckTime;
  699.     if ( timeDelta < DUCK_TIME) {
  700.         cg.refdef.vieworg[2] -= cg.duckChange 
  701.             * (DUCK_TIME - timeDelta) / DUCK_TIME;
  702.     }
  703.  
  704.     // add bob height
  705.     bob = cg.bobfracsin * cg.xyspeed * cg_bobup.value;
  706.     if (bob > 6) {
  707.         bob = 6;
  708.     }
  709.  
  710.     origin[2] += bob;
  711.  
  712.  
  713.     // add fall height
  714.     delta = cg.time - cg.landTime;
  715.     if ( delta < LAND_DEFLECT_TIME ) {
  716.         f = delta / LAND_DEFLECT_TIME;
  717.         cg.refdef.vieworg[2] += cg.landChange * f;
  718.     } else if ( delta < LAND_DEFLECT_TIME + LAND_RETURN_TIME ) {
  719.         delta -= LAND_DEFLECT_TIME;
  720.         f = 1.0 - ( delta / LAND_RETURN_TIME );
  721.         cg.refdef.vieworg[2] += cg.landChange * f;
  722.     }
  723.  
  724.     // add step offset
  725.     CG_StepOffset();
  726.  
  727.     // add kick offset
  728.  
  729.     if( cg.predictedPlayerState.pm_flags & PMF_LEANING )
  730.     {
  731.         float leanOffset;
  732.  
  733.         leanOffset = (float)(cg.predictedPlayerState.leanTime - LEAN_TIME) / LEAN_TIME * LEAN_OFFSET;
  734.         angles[ROLL] += leanOffset / 4;
  735.         AngleVectors( cg.predictedPlayerState.viewangles, NULL, right, NULL );
  736.         VectorMA( origin, leanOffset, right, origin );
  737.     }
  738.  
  739.     // Make sure view doesnt invert on itself
  740.     angles[PITCH] = Com_Clampf ( -89, 89, angles[PITCH] );
  741. }
  742.  
  743.  
  744. /*
  745. ====================
  746. CG_CalcFovFromX
  747.  
  748. Calcs Y FOV from given X FOV
  749. ====================
  750. */
  751. #define    WAVE_AMPLITUDE    1
  752. #define    WAVE_FREQUENCY    0.4
  753.  
  754. qboolean CG_CalcFOVFromX( float fov_x ) 
  755. {
  756.     float    x;
  757. //    float    phase;
  758. //    float    v;
  759. //    int        contents;
  760.     float    fov_y;
  761.     qboolean    inwater;
  762.  
  763.     x = cg.refdef.width / tan( fov_x / 360 * M_PI );
  764.     fov_y = atan2( cg.refdef.height, x );
  765.     fov_y = fov_y * 360 / M_PI;
  766.  
  767.     // there's a problem with this, it only takes the leafbrushes into account, not the entity brushes,
  768.     //    so if you give slime/water etc properties to a func_door area brush in order to move the whole water 
  769.     //    level up/down this doesn't take into account the door position, so warps the view the whole time
  770.     //    whether the water is up or not. Fortunately there's only one slime area in Trek that you can be under,
  771.     //    so lose it...
  772. #if 0
  773. /*
  774.     // warp if underwater
  775.     contents = CG_PointContents( cg.refdef.vieworg, -1 );
  776.     if ( contents & ( CONTENTS_WATER | CONTENTS_LAVA ) ){
  777.         phase = cg.time / 1000.0 * WAVE_FREQUENCY * M_PI * 2;
  778.         v = WAVE_AMPLITUDE * sin( phase );
  779.         fov_x += v;
  780.         fov_y -= v;
  781.         inwater = qtrue;
  782.     }
  783.     else {
  784.         inwater = qfalse;
  785.     }
  786. */
  787. #else
  788.     inwater = qfalse;
  789. #endif
  790.  
  791.  
  792.     // set it
  793.     cg.refdef.fov_x = fov_x;
  794.     cg.refdef.fov_y = fov_y;
  795.  
  796.     return (inwater);
  797. }
  798.  
  799. /*
  800. ====================
  801. CG_CalcFov
  802.  
  803. Fixed fov at intermissions, otherwise account for fov variable and zooms.
  804. ====================
  805. */
  806. #define    WAVE_AMPLITUDE    1
  807. #define    WAVE_FREQUENCY    0.4
  808.  
  809. static int CG_CalcFov( void ) {
  810.     float    x;
  811.     float    phase;
  812.     float    v;
  813.     int        contents;
  814.     float    fov_x, fov_y;
  815.     float    zoomFov;
  816.     float    f;
  817.     int        inwater;
  818.  
  819.     if ( cg.predictedPlayerState.pm_type == PM_INTERMISSION ) {
  820.         // if in intermission, use a fixed value
  821.         fov_x = 90;
  822.     }
  823.     else
  824.     {
  825.         // user selectable
  826.         if ( cgs.dmflags & DF_FIXED_FOV )
  827.         {
  828.             // dmflag to prevent wide fov for all clients
  829.             fov_x = 90;
  830.         }
  831.         else
  832.         {
  833.             fov_x = cg_fov.value;
  834.             if ( fov_x < 1 )
  835.             {
  836.                 fov_x = 1;
  837.             }
  838.             else if ( fov_x > 160 )
  839.             {
  840.                 fov_x = 160;
  841.             }
  842.         }
  843.  
  844.         // If cheats arent enabled then 80 is the lowest and 100 is the highest
  845.         if ( !cg.cheats )
  846.         {
  847.             if ( fov_x < 80 )
  848.             {
  849.                 fov_x = 80;
  850.             }
  851.             else if ( fov_x > 100 )
  852.             {
  853.                 fov_x = 100;
  854.             }
  855.         }
  856.  
  857.         if ( cg.predictedPlayerState.pm_flags & PMF_ZOOMED )
  858.         {
  859.             zoomFov = (float)cg.predictedPlayerState.zoomFov;
  860.  
  861.             if (!cg.predictedPlayerState.pm_flags & PMF_ZOOM_LOCKED )
  862.             {
  863.                 zoomFov -= cg.frametime * 0.05f;
  864.  
  865.                 if (zoomFov < MAX_ZOOM_FOV)
  866.                 {
  867.                     zoomFov = MAX_ZOOM_FOV;
  868.                 }
  869.                 else if (zoomFov > cg_fov.value)
  870.                 {
  871.                     zoomFov = cg_fov.value;
  872.                 }
  873.                 else
  874.                 {    // Still zooming
  875.                     static zoomSoundTime = 0;
  876.  
  877.                     if (zoomSoundTime < cg.time || zoomSoundTime > cg.time + 10000)
  878.                     {
  879.                         zoomSoundTime = cg.time + 300;
  880.                     }
  881.                 }
  882.             }
  883.  
  884.             fov_x = zoomFov;
  885.         }
  886.         else 
  887.         {
  888.             f = ( cg.time - cg.predictedPlayerState.zoomTime ) / ZOOM_OUT_TIME;
  889.             if ( f > 1.0 ) 
  890.             {
  891.                 fov_x = fov_x;
  892.             } 
  893.             else 
  894.             {
  895.                 fov_x = cg.predictedPlayerState.zoomFov + f * ( fov_x - cg.predictedPlayerState.zoomFov );
  896.             }
  897.         }
  898.     }
  899.  
  900.     x = cg.refdef.width / tan( fov_x / 360 * M_PI );
  901.     fov_y = atan2( cg.refdef.height, x );
  902.     fov_y = fov_y * 360 / M_PI;
  903.  
  904.     // warp if underwater
  905.     contents = CG_PointContents( cg.refdef.vieworg, -1 );
  906.     if ( contents & ( CONTENTS_WATER | CONTENTS_LAVA ) ){
  907.         phase = cg.time / 1000.0 * WAVE_FREQUENCY * M_PI * 2;
  908.         v = WAVE_AMPLITUDE * sin( phase );
  909.         fov_x += v;
  910.         fov_y -= v;
  911.         inwater = qtrue;
  912.     }
  913.     else {
  914.         inwater = qfalse;
  915.     }
  916.  
  917.     // set it
  918.     cg.refdef.fov_x = fov_x;
  919.     cg.refdef.fov_y = fov_y;
  920.  
  921.     if ( !(cg.predictedPlayerState.pm_flags&PMF_ZOOMED) )
  922.     {
  923.         cg.zoomSensitivity = 1;
  924.     } 
  925.     else
  926.     {
  927.         cg.zoomSensitivity = cg.refdef.fov_y / 75.0;
  928.     }
  929.  
  930.     return inwater;
  931. }
  932.  
  933. static void CG_DamageBlendBlob( void )
  934. {
  935.     int            t = 250;
  936.     int            maxTime = DAMAGE_TIME;
  937.     vec3_t        lineStart;
  938.     vec3_t        lineEnd;
  939.     vec3_t        rgb1;
  940.     vec3_t        rgb2;
  941.     qboolean    dmgIsToTheLeft;
  942.     qboolean    dmgIsAbove;
  943.     qboolean    unknownDir;
  944.     float        alphaVal;
  945.     float        cvarScale;
  946.  
  947.     if (cg_damageindicator.value <= 0) 
  948.     {
  949.         return;
  950.     }
  951.  
  952.     t = cg.time - cg.damageTime;
  953.     if ( t <= 0 || t >= maxTime )
  954.     {
  955.         return;
  956.     }
  957.  
  958.     // here we use cg.damageY as a value to indicate how much above you the damage is and
  959.     //cg.damageX as a value to indicate how much to your left the damage is
  960.  
  961.     // draw a "line" using the cgs.media.damageDirShader
  962.     dmgIsToTheLeft  = (cg.damageX > 0);
  963.     dmgIsAbove        = (cg.damageY < 0);
  964.     unknownDir        = (cg.v_dmg_roll == 255);
  965.     alphaVal        = ( 1.0 - ((float)t / maxTime) );
  966.     cvarScale        = 1.0f * cg_damageindicator.value;
  967.  
  968.     VectorSet(rgb1, 1, 1, 1);
  969.     VectorSet(rgb2, 1, 1, 1);
  970.  
  971.     // draw the left/right indicator if it's far enough from center
  972.     if ( (fabs(cg.damageX) > 25) || unknownDir)
  973.     {
  974.         VectorMA(cg.refdef.vieworg, 20, cg.refdef.viewaxis[0], lineStart);
  975.         VectorMA(lineStart, (dmgIsToTheLeft?6:-6), cg.refdef.viewaxis[1], lineStart);
  976.         VectorMA(lineStart, (dmgIsToTheLeft?2:-2)*cvarScale, cg.refdef.viewaxis[1], lineEnd);
  977.  
  978.         trap_FX_AddLine( lineStart, lineEnd, 
  979.                             5.0f*cvarScale, 5.0f*cvarScale, 0.0f,
  980.                             alphaVal, alphaVal, 0.0f,
  981.                             rgb1, rgb2, 0.0f,
  982.                             1, cgs.media.damageDirShader, FX_DEPTH_HACK );
  983.     }
  984.     if (unknownDir)
  985.     {
  986.         // dmg from unknown direction...display all four directions
  987.         VectorMA(cg.refdef.vieworg, 20, cg.refdef.viewaxis[0], lineStart);
  988.         VectorMA(lineStart, (dmgIsToTheLeft?-6:6), cg.refdef.viewaxis[1], lineStart);
  989.         VectorMA(lineStart, (dmgIsToTheLeft?-2:2)*cvarScale, cg.refdef.viewaxis[1], lineEnd);
  990.  
  991.         trap_FX_AddLine( lineStart, lineEnd, 
  992.                             5.0f*cvarScale, 5.0f*cvarScale, 0.0f,
  993.                             alphaVal, alphaVal, 0.0f,
  994.                             rgb1, rgb2, 0.0f,
  995.                             1, cgs.media.damageDirShader, FX_DEPTH_HACK );
  996.     }
  997.  
  998.     // draw the above/below indicator
  999.     if ( (fabs(cg.damageY) > 15) || unknownDir)
  1000.     {
  1001.         VectorMA(cg.refdef.vieworg, 20, cg.refdef.viewaxis[0], lineStart);
  1002.         VectorMA(lineStart, (dmgIsAbove?6:-6), cg.refdef.viewaxis[2], lineStart);
  1003.         VectorMA(lineStart, (dmgIsAbove?2:-2)*cvarScale, cg.refdef.viewaxis[2], lineEnd);
  1004.  
  1005.         trap_FX_AddLine( lineStart, lineEnd, 
  1006.                             5.0f*cvarScale, 5.0f*cvarScale, 0.0f,
  1007.                             alphaVal, alphaVal, 0.0f,
  1008.                             rgb1, rgb2, 0.0f,
  1009.                             1, cgs.media.damageDirShader, FX_DEPTH_HACK );
  1010.     }
  1011.     if (unknownDir)
  1012.     {
  1013.         VectorMA(cg.refdef.vieworg, 20, cg.refdef.viewaxis[0], lineStart);
  1014.         VectorMA(lineStart, (dmgIsAbove?-6:6), cg.refdef.viewaxis[2], lineStart);
  1015.         VectorMA(lineStart, (dmgIsAbove?-2:2)*cvarScale, cg.refdef.viewaxis[2], lineEnd);
  1016.  
  1017.         trap_FX_AddLine( lineStart, lineEnd, 
  1018.                             5.0f*cvarScale, 5.0f*cvarScale, 0.0f,
  1019.                             alphaVal, alphaVal, 0.0f,
  1020.                             rgb1, rgb2, 0.0f,
  1021.                             1, cgs.media.damageDirShader, FX_DEPTH_HACK );
  1022.     }
  1023. }
  1024.  
  1025. /*
  1026. ===============
  1027. CG_CalcViewValues
  1028.  
  1029. Sets cg.refdef view values
  1030. ===============
  1031. */
  1032. static int CG_CalcViewValues( void ) 
  1033. {
  1034.     playerState_t    *ps;
  1035.  
  1036.     memset( &cg.refdef, 0, sizeof( cg.refdef ) );
  1037.  
  1038.     // strings for in game rendering
  1039.     // Q_strncpyz( cg.refdef.text[0], "Park Ranger", sizeof(cg.refdef.text[0]) );
  1040.     // Q_strncpyz( cg.refdef.text[1], "19", sizeof(cg.refdef.text[1]) );
  1041.  
  1042.     // calculate size of 3D view
  1043.     CG_CalcVrect();
  1044.  
  1045.     ps = &cg.predictedPlayerState;
  1046. /*
  1047.     if (cg.cameraMode) {
  1048.         vec3_t origin, angles;
  1049.         if (trap_getCameraInfo(cg.time, &origin, &angles)) {
  1050.             VectorCopy(origin, cg.refdef.vieworg);
  1051.             angles[ROLL] = 0;
  1052.             VectorCopy(angles, cg.refdef.viewangles);
  1053.             AnglesToAxis( cg.refdef.viewangles, cg.refdef.viewaxis );
  1054.             return CG_CalcFov();
  1055.         } else {
  1056.             cg.cameraMode = qfalse;
  1057.         }
  1058.     }
  1059. */
  1060.     // intermission view
  1061.     if ( ps->pm_type == PM_INTERMISSION ) 
  1062.     {
  1063.         VectorCopy( ps->origin, cg.refdef.vieworg );
  1064.         VectorCopy( ps->viewangles, cg.refdef.viewangles );
  1065.         AnglesToAxis( cg.refdef.viewangles, cg.refdef.viewaxis );
  1066.         return CG_CalcFov();
  1067.     }
  1068.  
  1069.     cg.bobcycle = ( ps->bobCycle & 128 ) >> 7;
  1070.     cg.bobfracsin = fabs( sin( ( ps->bobCycle & 127 ) / 127.0 * M_PI ) );
  1071.     cg.xyspeed = sqrt( ps->velocity[0] * ps->velocity[0] +
  1072.         ps->velocity[1] * ps->velocity[1] );
  1073.  
  1074.  
  1075.     VectorCopy( ps->origin, cg.refdef.vieworg );
  1076.     VectorCopy( ps->viewangles, cg.refdef.viewangles );
  1077.  
  1078.     if (cg_cameraOrbit.integer) 
  1079.     {
  1080.         if (cg.time > cg.nextOrbitTime) 
  1081.         {
  1082.             cg.nextOrbitTime = cg.time + cg_cameraOrbitDelay.integer;
  1083.             cg_thirdPersonYaw.value += cg_cameraOrbit.value;
  1084.         }
  1085.     }
  1086.  
  1087.     // add error decay
  1088.     if ( cg_errorDecay.value > 0 ) 
  1089.     {
  1090.         int        t;
  1091.         float    f;
  1092.  
  1093.         t = cg.time - cg.predictedErrorTime;
  1094.         f = ( cg_errorDecay.value - t ) / cg_errorDecay.value;
  1095.         if ( f > 0 && f < 1 ) {
  1096.             VectorMA( cg.refdef.vieworg, f, cg.predictedError, cg.refdef.vieworg );
  1097.         } else {
  1098.             cg.predictedErrorTime = 0;
  1099.         }
  1100.     }
  1101.  
  1102.     if ( cg.renderingThirdPerson && !(cg.snap->ps.pm_flags & PMF_ZOOMED) ) 
  1103.     {
  1104.         // back away from character
  1105.         CG_OffsetThirdPersonView();
  1106.     } 
  1107.     else 
  1108.     {
  1109.         // offset for local bobbing and kicks
  1110.         CG_OffsetFirstPersonView();
  1111.     }
  1112.  
  1113.     CG_UpdateCameraShake ( cg.refdef.vieworg, cg.refdef.viewangles );
  1114.  
  1115.     // position eye reletive to origin
  1116.     AnglesToAxis( cg.refdef.viewangles, cg.refdef.viewaxis );
  1117.  
  1118.     if ( cg.hyperspace ) {
  1119.         cg.refdef.rdflags |= RDF_NOWORLDMODEL | RDF_HYPERSPACE;
  1120.     }
  1121.  
  1122.     // field of view
  1123.     return CG_CalcFov();
  1124. }
  1125.  
  1126. /*
  1127. =====================
  1128. CG_AddBufferedSound
  1129. =====================
  1130. */
  1131. void CG_AddBufferedSound( sfxHandle_t sfx ) {
  1132.     if ( !sfx )
  1133.         return;
  1134.     cg.soundBuffer[cg.soundBufferIn] = sfx;
  1135.     cg.soundBufferIn = (cg.soundBufferIn + 1) % MAX_SOUNDBUFFER;
  1136.     if (cg.soundBufferIn == cg.soundBufferOut) {
  1137.         cg.soundBufferOut++;
  1138.     }
  1139. }
  1140.  
  1141. /*
  1142. =====================
  1143. CG_PlayBufferedSounds
  1144. =====================
  1145. */
  1146. static void CG_PlayBufferedSounds( void ) {
  1147.     if ( cg.soundTime < cg.time ) {
  1148.         if (cg.soundBufferOut != cg.soundBufferIn && cg.soundBuffer[cg.soundBufferOut]) {
  1149.             trap_S_StartLocalSound(cg.soundBuffer[cg.soundBufferOut], CHAN_ANNOUNCER);
  1150.             cg.soundBuffer[cg.soundBufferOut] = 0;
  1151.             cg.soundBufferOut = (cg.soundBufferOut + 1) % MAX_SOUNDBUFFER;
  1152.             cg.soundTime = cg.time + 750;
  1153.         }
  1154.     }
  1155. }
  1156.  
  1157. //=========================================================================
  1158.  
  1159. /*
  1160. =================
  1161. CG_UpdateRogerWilco
  1162.  
  1163. Updates the roger wilco team you are on
  1164. =================
  1165. */
  1166. void CG_UpdateRogerWilco ( void )
  1167. {
  1168.     qboolean dead = qfalse;
  1169.  
  1170.     if ( !rw_enabled.integer )
  1171.     {
  1172.         return;
  1173.     }
  1174.  
  1175.     if ( cgs.clientinfo[cg.clientNum].team != TEAM_SPECTATOR )
  1176.     {
  1177.         if ( cgs.gametypeData->respawnType == RT_NONE )
  1178.         {
  1179.             if ( cg.predictedPlayerState.pm_flags & (PMF_GHOST|PMF_FOLLOW) || cg.predictedPlayerState.pm_type == PM_DEAD )
  1180.             {
  1181.                 dead = qtrue;
  1182.             }
  1183.         }        
  1184.     }
  1185.  
  1186.     trap_RW_SetTeam(cgs.clientinfo[cg.clientNum].team, dead);
  1187. }
  1188.  
  1189. /*
  1190. =================
  1191. CG_DrawActiveFrame
  1192.  
  1193. Generates and draws a game scene and status information at the given time.
  1194. =================
  1195. */
  1196. void CG_DrawActiveFrame( int serverTime, stereoFrame_t stereoView, qboolean demoPlayback ) {
  1197.     int        inwater;
  1198.  
  1199.     cg.time = serverTime;
  1200.     cg.demoPlayback = demoPlayback;
  1201.  
  1202.     // update cvars
  1203.     CG_UpdateCvars();
  1204.  
  1205.     // if we are only updating the screen as a loading
  1206.     // pacifier, don't even try to read snapshots
  1207.     if ( cg.infoScreenText[0] != 0 ) {
  1208.         CG_DrawInformation();
  1209.         return;
  1210.     }
  1211.  
  1212.     trap_FX_AdjustTime( cg.time );
  1213.  
  1214.     CG_UpdateRogerWilco ( );
  1215.  
  1216.     CG_RunLightStyles();
  1217.  
  1218.     // any looped sounds will be respecified as entities
  1219.     // are added to the render list
  1220.     trap_S_ClearLoopingSounds(qfalse);
  1221.  
  1222.     // clear all the render lists
  1223.     trap_R_ClearScene();
  1224.  
  1225.     // set up cg.snap and possibly cg.nextSnap
  1226.     CG_ProcessSnapshots();
  1227.  
  1228.     // if we haven't received any snapshots yet, all
  1229.     // we can draw is the information screen
  1230.     if ( !cg.snap || ( cg.snap->snapFlags & SNAPFLAG_NOT_ACTIVE ) ) {
  1231.         CG_DrawInformation();
  1232.         return;
  1233.     }
  1234.  
  1235.     // Handle last weapon selection
  1236.     if ( cg.weaponSelect != cg.weaponOldSelect )
  1237.     {
  1238.         cg.weaponLastSelect = cg.weaponOldSelect;
  1239.         cg.weaponOldSelect = cg.weaponSelect;
  1240.     }
  1241.  
  1242.     // let the client system know what our weapon and zoom settings are
  1243.     if ( cg.weaponMenuUp )
  1244.     {
  1245.         trap_SetUserCmdValue( (cg.weaponMenuSelect | WP_DELAYED_CHANGE_BIT), cg.zoomSensitivity );
  1246.     }
  1247.     else
  1248.     {
  1249.         trap_SetUserCmdValue( cg.weaponSelect, cg.zoomSensitivity );
  1250.     }
  1251.  
  1252.     // this counter will be bumped for every valid scene we generate
  1253.     cg.clientFrame++;
  1254.  
  1255.     // update cg.predictedPlayerState
  1256.     CG_PredictPlayerState();
  1257.  
  1258.     // update the scores from the playerstate
  1259.     if ( cgs.scores1 != cg.predictedPlayerState.persistant[PERS_RED_SCORE] )
  1260.     {        
  1261.         cgs.scores1 = cg.predictedPlayerState.persistant[PERS_RED_SCORE];
  1262.         trap_Cvar_Set ( "ui_info_redscore", va("%d", cgs.scores1 ) );
  1263.     }
  1264.  
  1265.     if ( cgs.scores2 != cg.predictedPlayerState.persistant[PERS_BLUE_SCORE] )
  1266.     {
  1267.         cgs.scores2 = cg.predictedPlayerState.persistant[PERS_BLUE_SCORE];
  1268.         trap_Cvar_Set ( "ui_info_bluescore", va("%d", cgs.scores2 ) );
  1269.     }
  1270.  
  1271.     // decide on third person view
  1272.     if ( cg.snap->ps.persistant[PERS_TEAM] == TEAM_SPECTATOR || (cg.snap->ps.pm_flags & PMF_GHOST))
  1273.     {
  1274.         cg.renderingThirdPerson = qfalse;
  1275.     }
  1276.     else if ( cg.snap->ps.pm_type == PM_DEAD )
  1277.     {
  1278.         cg.renderingThirdPerson = cg_thirdPerson.integer;
  1279.     }
  1280.     else
  1281.     {
  1282.         if ( cg.snap->ps.pm_flags & PMF_ZOOMED )
  1283.         {
  1284.             cg.renderingThirdPerson = qfalse;
  1285.         }
  1286.         else if ( cg.snap->ps.pm_flags & PMF_FOLLOW )
  1287.         {
  1288.             cg.renderingThirdPerson = qtrue;
  1289.         }
  1290.         else
  1291.         {
  1292.             cg.renderingThirdPerson = cg_thirdPerson.integer || (cg.snap->ps.stats[STAT_HEALTH] <= 0);
  1293.         }
  1294.     }
  1295.  
  1296.     // build cg.refdef
  1297.     inwater = CG_CalcViewValues();
  1298.     
  1299.     // first person blend blobs, done after AnglesToAxis
  1300.     if ( !cg.renderingThirdPerson ) {
  1301.         CG_DamageBlendBlob();
  1302.     }
  1303.  
  1304.     // Load deferred models
  1305.     if ( cg.deferredPlayerLoading > 10 ) 
  1306.     {
  1307.         CG_LoadDeferredPlayers();
  1308.         cg.deferredPlayerLoading = 0;
  1309.     }
  1310.  
  1311.     // build the render lists
  1312.     if ( !cg.hyperspace ) 
  1313.     {
  1314.         CG_AddPacketEntities();            // adter calcViewValues, so predicted player state is correct
  1315.         CG_AddLocalEntities();
  1316.         CG_DrawMiscEnts();
  1317.     }
  1318.  
  1319.     if ( cg.snap->ps.stats[STAT_HEALTH] > 0 )
  1320.     {
  1321.         CG_AnimateViewWeapon(&cg.predictedPlayerState);
  1322.         CG_AddViewWeapon( &cg.predictedPlayerState );
  1323.     }
  1324.  
  1325.     if ( !cg.hyperspace ) 
  1326.     {
  1327.         trap_FX_AddScheduledEffects();
  1328.     }
  1329.  
  1330.     // add buffered sounds
  1331.     CG_PlayBufferedSounds();
  1332.  
  1333.     // play buffered voice chats
  1334.     CG_PlayBufferedVoiceChats();
  1335.  
  1336.     // finish up the rest of the refdef
  1337.     if ( cg.testModelEntity.hModel ) {
  1338.         CG_AddTestModel();
  1339.     }
  1340.     cg.refdef.time = cg.time;
  1341.     memcpy( cg.refdef.areamask, cg.snap->areamask, sizeof( cg.refdef.areamask ) );
  1342.  
  1343.     // update audio positions
  1344.     if ( !cg.mMapChange )
  1345.     {
  1346.         trap_AS_UpdateAmbientSet( CG_ConfigString( CS_AMBIENT_SOUNDSETS ), cg.refdef.vieworg );    // MUST be before trap_S_Respatialize()!    -ste
  1347.         trap_S_Respatialize( cg.snap->ps.clientNum, cg.refdef.vieworg, cg.refdef.viewaxis, inwater );
  1348.     }
  1349.  
  1350.     // make sure the lagometerSample and frame timing isn't done twice when in stereo
  1351.     if ( stereoView != STEREO_RIGHT ) {
  1352.         cg.frametime = cg.time - cg.oldTime;
  1353.         if ( cg.frametime < 0 ) {
  1354.             cg.frametime = 0;
  1355.         }
  1356.         cg.oldTime = cg.time;
  1357.         CG_AddLagometerFrameInfo();
  1358.     }
  1359.  
  1360.     // actually issue the rendering calls
  1361.     CG_DrawActive( stereoView );
  1362.  
  1363.     if ( cg_stats.integer ) 
  1364.     {
  1365.         Com_Printf( "cg.clientFrame:%i\n", cg.clientFrame );
  1366.     }
  1367. }
  1368.  
  1369. /*
  1370. =================
  1371. CG_CameraShake
  1372.  
  1373. Shakes the camera a bit (used in grenades)
  1374. =================
  1375. */
  1376. #define MAX_SHAKE_INTENSITY            2.0f
  1377. #define DEFAULT_EXPLOSION_RADIUS    400
  1378.  
  1379. void CG_CameraShake ( float* origin, float intensity, int radius, int time )
  1380. {
  1381.     vec3_t    dir;
  1382.     float    dist, intensityScale;
  1383.     float    realIntensity;
  1384.  
  1385.     VectorSubtract( cg.refdef.vieworg, origin, dir );
  1386.     dist = VectorNormalize( dir );
  1387.  
  1388.     // Apparently the SoF2 camera shake function takes a time, but not a radius.
  1389.     //    when someone feels like fixing this, we can be a bit more flexible
  1390.     radius = DEFAULT_EXPLOSION_RADIUS;
  1391.  
  1392.     //Use the dir to add kick to the explosion
  1393.     if ( dist > radius )
  1394.         return;
  1395.  
  1396.     intensityScale = 1 - ( dist / (float) radius );
  1397.     realIntensity = intensity * intensityScale;
  1398.  
  1399.     if ( realIntensity > MAX_SHAKE_INTENSITY )
  1400.         realIntensity = MAX_SHAKE_INTENSITY;
  1401.  
  1402.     cg.shakeIntensity = realIntensity;
  1403.     cg.shakeDuration = time;
  1404.     cg.shakeStart = cg.time;
  1405. }
  1406.  
  1407. /*
  1408. =================
  1409. CG_UpdateCameraShake
  1410.  
  1411. Updates the camera shake
  1412. =================
  1413. */
  1414. void CG_UpdateCameraShake ( vec3_t origin, vec3_t angles )
  1415. {
  1416.     vec3_t    moveDir;
  1417.     float    intensity_scale;
  1418.     float    intensity;
  1419.     int        i;
  1420.  
  1421.     if ( cg.shakeDuration <= 0 )
  1422.         return;
  1423.  
  1424.     if ( cg.time > ( cg.shakeStart + cg.shakeDuration ) )
  1425.     {
  1426.         cg.shakeIntensity = 0;
  1427.         cg.shakeDuration = 0;
  1428.         cg.shakeStart = 0;
  1429.         return;
  1430.     }
  1431.  
  1432.     //intensity_scale now also takes into account FOV with 90.0 as normal
  1433.     intensity_scale = 1.0f - ( (float) ( cg.time - cg.shakeStart ) / (float) cg.shakeDuration ) * (((cg.refdef.fov_x+cg.refdef.fov_y)/2.0f)/90.0f);
  1434.     intensity = cg.shakeIntensity * intensity_scale;
  1435.  
  1436.     for ( i = 0; i < 3; i++ )
  1437.     {
  1438.         moveDir[i] = flrand(-intensity, intensity );
  1439.     }
  1440.  
  1441.     //FIXME: Lerp
  1442.  
  1443.     //Move the camera
  1444.     VectorAdd( origin, moveDir, origin );
  1445.  
  1446.     for ( i = 0; i < 3; i++ )
  1447.     {
  1448.         moveDir[i] = flrand(-intensity, intensity );
  1449.     }
  1450.  
  1451.     //FIXME: Lerp
  1452.  
  1453.     //Move the angles
  1454.     VectorAdd( angles, moveDir, angles );
  1455. }
  1456.